package org.sn.jdish.example.queue;

/**
 * 两个队列实现一个栈
 * 
 * @author snzigod@hotmail.com
 *
 */
public class Queue2Stack {

	private Queue queue1 = new Queue();
	private Queue queue2 = new Queue();

	int size = 0;

	public void push(Object data) {
		queue1.enQueue(data);
		size++;
	}

	public Object pop() {
		Object data;
		if (queue1.empty()) {
			return null;
		}
		size--;
		while (!queue1.empty()) {
			if (queue1.size == 1) {
				data = queue1.deQueue();
				while (!queue2.empty()) {
					queue1.enQueue(queue2.deQueue());
				}
				return data;
			}
			queue2.enQueue(queue1.deQueue());
		}
		return null;
	}

	public boolean empty() {
		return size == 0;
	}

	public static void main(String[] args) {
		Queue2Stack stack = new Queue2Stack();
		for (int i = 0; i < 5; i++) {
			stack.push(i);
		}
		while (!stack.empty()) {
			System.out.println(stack.pop());
		}
	}
}
